//
//  MyTabViewController.swift
//  Wechat1.0
//
//  Created by henaihan on 2017/11/6.
//  Copyright © 2017年 henaihan. All rights reserved.
//

import UIKit

class MyTabViewController: UITabBarController {
    var tabbarBack : UIView!
    var lastBtn : UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadViewControllers()
        self.tabbarBack = UIView(frame  : self.tabBar.frame)
        self.tabbarBack.backgroundColor = UIColor.white
        self.tabbarBack.backgroundColor?.withAlphaComponent(0.9)
        self.view.addSubview(self.tabbarBack)
        self.tabBar.removeFromSuperview()
        
        self.initTabBar()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func loadViewControllers(){
        let chatVC: ChatTableViewController = ChatTableViewController()
        //第一页 聊天
        let chatNC  = UINavigationController(rootViewController: chatVC)
        // 第二页 通讯录
        let contactsVC: ContancetTableViewController = ContancetTableViewController()
        let contactsNC = UINavigationController(rootViewController: contactsVC)
        //发现
        let exploreVC: ExploreTableViewController = ExploreTableViewController()
        let exploreNC = UINavigationController(rootViewController: exploreVC)
        //我
        let meVC: MyTableViewController = MyTableViewController()
        let meNC: UINavigationController = UINavigationController(rootViewController: meVC)
        
        //新建一个导航控制器的实例的数组
        let controllersArray: NSArray = [chatNC, contactsNC, exploreNC, meNC]
        
        //把导航控制器的数组加入到标签控制器
        self.setViewControllers(controllersArray as? [UIViewController], animated: true)
        
        for i in 0..<controllersArray.count
        {
            (controllersArray[i] as! UINavigationController).navigationBar.backgroundColor = UIColor.red
            (controllersArray[i] as! UINavigationController).navigationBar.barStyle = .blackTranslucent
            (controllersArray[i] as! UINavigationController).navigationBar.tintColor = UIColor.white
        }
    }
    
        
        
        
        func btnClick(_ btn: UIButton)
        {
            self.lastBtn.isSelected = false
            btn.isSelected = true
            self.selectedIndex = btn.tag
            self.lastBtn = btn
        }
        
        func initTabBar()
            
        {
            let btnWidth : Int = Int(self.tabBar.frame.size.width)/self.viewControllers!.count
            for i in 0..<viewControllers!.count
            {
                let btn : UIButton = UIButton(type: .custom)
                let rect : CGRect = CGRect(x : btnWidth*i , y :4,width: btnWidth , height : 40
            )
                btn.frame = rect
                btn.setImage(UIImage(named: "tabbar\(i+1)") , for: UIControlState.normal)
                btn.setImage(UIImage(named: "tabbarsel\(i+1)") , for: UIControlState.selected)
                switch i
                {
                    case 0:btn.setTitle("聊天", for: UIControlState.normal)
                    case 1:btn.setTitle("联系人", for: UIControlState.normal)
                    case 2:btn.setTitle("发现", for: UIControlState.normal)
                    case 3:btn.setTitle("我", for: UIControlState.normal)
                    default:print("error")
                }
                btn.titleLabel?.font = UIFont.systemFont(ofSize : 10)
                btn.setTitleColor(UIColor.init(colorLiteralRed: 100.0/255.0, green: 173.0/255.0, blue: 25.0/255.0, alpha: 1.0),for: UIControlState.normal)
                btn.setTitleColor(UIColor.init(colorLiteralRed: 26.0/255.0, green: 173.0/255.0, blue: 25.0/255.0, alpha: 1.0),for: UIControlState.selected)
                btn.tag = i
                btn.addTarget(self, action: #selector(btnClick(_:)), for: UIControlEvents.touchUpInside)
                let totalHeight: CGFloat = (btn.titleLabel!.frame.size.height + btn.imageView!.frame.size.height)
                // 设置按钮图片偏移
                btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: -(btn.titleLabel?.frame.size.width)!)
                
                // 设置按钮标题偏移
                btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(btn.imageView?.frame.size.width)!, bottom: -(totalHeight - (btn.titleLabel?.frame.size.height)!)-8, right: 0)
                self.tabbarBack.addSubview(btn)
                if i == 0 {
                    btn.isSelected = true
                    self.lastBtn = btn
                }

            }
            
            
            
        }

    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */


